function checkUsername(username){
    if(username.length===0){
        return '① 用户名不能为空';
    }else if(username.length<=1){
        return '① 用户名的长度不能小于1';
    }else{
        return '';
    }
}

function checkPassword(password){
    if(password.length===0){
        return '① 密码不能为空';
    }else if(password.length<=1){
        return '① 密码的长度不能小于1';
    }else{
        return '';
    }
}


$(function (){

    //判断注册页面的用户名是否可用
    $('#registerUsername').on('blur',function () {
        var registerUsername = $(this).val();
        var validateMsg = checkUsername(registerUsername);
        if(validateMsg === ''){
            $('#usernameMsg').text('');
            $.ajax({
                type    :'GET',
                url     :'isUsernameExist?registerUsername='+registerUsername,
                success :function (data){
                    //data="Exist"
                    console.log(data);
                    if(data === 'Exist')
                        $('#usernameMsg').text('① 用户名已存在');
                },
                error   :function (errorMsg) {
                    console.log(errorMsg);
                }
            })
        }else{
            $('#usernameMsg').text(validateMsg);
        }
    })
    //判断注册页面的密码是否可用
    $('#registerPassword').on('blur',function () {
        var password = $(this).val();
        if(checkPassword(password)===''){
            $('#passwordMsg').text('');
        }else{
            $('#passwordMsg').text(checkPassword(password))
        }
    });


    //判断登录页面的用户名是否可用
    $('#loginUsername').on('blur',function () {
        var username = $(this).val();
        if(checkUsername(username) === ''){
            $('#usernameMsg').text('');
            $.ajax({
                type    :'GET',
                url     :'isUsernameExist?username='+username,
                success :function (data){
                    //data="Exist"
                    console.log(data);
                    if(data === 'Not Exist')
                        $('#usernameMsg').text('① 用户名不存在');
                },
                error   :function (errorMsg) {
                    console.log(errorMsg);
                }
            })
        }else{
            $('#usernameMsg').text(checkUsername(username));
        }
    })
    //判断登录页面的密码是否合法
    $('#loginPassword').on('blur',function (){
        var password = $(this).val();
        if(checkPassword(password)===''){
            $('#passwordMsg').text('');
        }else{
            $('#passwordMsg').text(checkPassword(password))
        }
    })



    //点击登录按钮
    $('#loginBtn').on('click',function () {
        var username = $('#loginUsername').val();
        console.log(username);
        var password = $('#loginPassword').val();
        console.log(password);
        var selectManager = $('#selectManager').is(':checked')?1:0;
        console.log(selectManager);
        var inputCode = $('#inputCode').val();
        console.log(inputCode);

        if(checkPassword(password)===''){
            $('#passwordMsg').text('');
            $.ajax({
                type    :'GET',
                url     :'isPasswordValidate',
                data    :{"username":username,"password":password,'selectManager':selectManager,'inputCode':inputCode},
                success :function (data){
                    console.log(data);
                    if(data === 'Verify Not Validate'){
                        confirm('验证码不正确，请重新输入！');
                        //TODO:jsp页面刷新验证码

                        $(document).preventDefault();
                    }
                    if(data === 'Password Not Validate'){
                        $('#passwordMsg').text('① 密码错误');
                    }else if(data === 'Status Not Validate'){
                        confirm('您不是管理员！');
                    }else{
                        console.log('身份和账号合法');
                        //进入首页
                        location.href = 'bookHome'

                        //向sessionStorage中存放账号用户名
                        sessionStorage.setItem('accountName',username);
                        //向sessionStorage中存放账号密码
                        sessionStorage.setItem('accountPassword',password);
                        //向sessionStorage中存放账号身份
                        sessionStorage.setItem('accountSelectManager',selectManager.toString(10));
                        if(data === 'Manager Validate'){
                            console.log('管理员身份选择用户界面');
                            //向sessionStorage中存放账号真实身份
                            sessionStorage.setItem('accountIsManager','1');
                        }else{
                            sessionStorage.setItem('accountIsManager','0');
                        }
                    }
                },
                error   :function (errorMsg) {
                    console.log(errorMsg);
                }
            })

        }else{
            $('#passwordMsg').text(checkPassword(password))
        }
    });






















})










